package Exp2_ChenGY;

import java.util.ArrayList;  
import java.util.List;  
  
class BankCard {  
    private String id;  
    private String accountName;  
    private String password;  
    private double balance;  
    private String bankName;  
  
    // 构造方法、getter和setter方法（省略了具体的实现）  
  
    public BankCard(String id, String accountName, String password, double balance, String bankName) {  
        this.id = id;  
        this.accountName = accountName;  
        this.password = password;  
        this.balance = balance;  
        this.bankName = bankName;  
    }  
  
    public boolean verifyPassword(String password) {  
        return this.password.equals(password);  
    }  
  
    public boolean withdraw(double amount) {  
        if (this.balance >= amount) {  
            this.balance -= amount;  
            return true;  
        }  
        return false;  
    }  
  
    public double getBalance() {  
        return this.balance;  
    }  
  
    public String getBankName() {  
        return this.bankName;  
    }  
  
    // 添加toString方法以便于输出信息  
    @Override  
    public String toString() {  
        return "BankCard{" +  
                "id='" + id + '\'' +  
                ", accountName='" + accountName + '\'' +  
                ", balance=" + balance +  
                ", bankName='" + bankName + '\'' +  
                '}';  
    }  
} 
  
class ConsumerWallet {  
    private String walletPassword;  
    private List<BankCard> bankCards = new ArrayList<>();  
  
    public ConsumerWallet(String walletPassword) {  
        this.walletPassword = walletPassword;  
    }  
  
    public boolean addBankCard(BankCard card, String cardPassword) {  
        return card.verifyPassword(cardPassword) && bankCards.add(card);  
    }  
  
    public boolean verifyWalletPassword(String password) {  
        return this.walletPassword.equals(password);  
    }  
  
    public boolean pay(double amount) {  
        if (amount <= 0 || !verifyWalletPassword(this.walletPassword)) {  
            System.out.println("支付失败：金额无效或钱包密码错误！");  
            return false;  
        }  
  
        double remainingAmount = amount;  
        StringBuilder paymentInfo = new StringBuilder();  
  
        for (BankCard card : bankCards) {  
            if (remainingAmount > 0) {  
                double withdrawAmount = Math.min(remainingAmount, card.getBalance());  
                if (card.withdraw(withdrawAmount)) {  
                    remainingAmount -= withdrawAmount;  
                    paymentInfo.append("从").append(card.getBankName()).append("扣除").append(withdrawAmount).append("元；");  
                }  
            }  
        }  
  
        if (remainingAmount == 0) {  
            System.out.println("支付成功：" + paymentInfo.toString().trim());  
            return true;  
        } else {  
            System.out.println("支付失败：余额不足！");  
            return false;  
        }  
    }  
  
    // 添加toString方法以便于输出信息  
    @Override  
    public String toString() {  
        return "ConsumerWallet{" +  
                "walletPassword='" + walletPassword + '\'' +  
                ", bankCards=" + bankCards +  
                '}';  
    }  
    
    public void withdrawFromCard(BankCard card, double amount, String cardPassword) {  
        if (card.verifyPassword(cardPassword) && card.withdraw(amount)) {  
            System.out.println("成功从银行卡中取款 " + amount + " 元。");  
        } else {  
            System.out.println("取款失败，请检查密码或余额。");  
        }  
    } 
    
}
  
public class PayPal {  
  
    public static void main(String[] args) {  
        // 初始化银行卡  
        BankCard card = new BankCard("1234567890", "张三", "123456", 1000.0, "某银行");  
  
        // 初始化钱包  
        ConsumerWallet wallet = new ConsumerWallet("654321");  
  
        // 尝试向钱包中添加银行卡（这里假设卡密码验证通过）  
        if (wallet.addBankCard(card, "123456")) {  
            System.out.println("银行卡已成功添加到钱包中。");  
        }  
  
        // 验证钱包密码  
        if (wallet.verifyWalletPassword("654321")) {  
            System.out.println("钱包密码验证成功。");  
  
            // 尝试从银行卡中取款  
            wallet.withdrawFromCard(card, 500.0, "123456");  
        }  
  
        // 输出银行卡信息  
        System.out.println("银行卡信息: " + card);  
    }  
}